package com.linafri;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
import java.util.concurrent.TimeUnit;

/**
 * TODO 类实现描述
 *
 * @author wb_yanghui
 * @since 2018年8月10日 下午3:43:39
 */
public class CountDownLatchDemo implements Runnable {
    static final CountDownLatch     end  = new CountDownLatch(10);

    static final CountDownLatchDemo demo = new CountDownLatchDemo();

    @Override
    public void run() {

        try {
            Thread.sleep(new Random().nextInt(10) * 1000);
            System.out.println(Thread.currentThread().getId() + "检查完成");
            end.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 10, 0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(), new AbortPolicy());
        for (int i = 0; i < 10; i++) {
            System.out.println(demo);
            threadPoolExecutor.submit(demo);
        }

        end.await();
        System.out.println("Fire!");

        threadPoolExecutor.shutdown();
    }
}
